<template>
  <div class="companyDepart">
    <el-cascader
      :props="props"
      @change="handleChange"
      v-model="value"
      :show-all-levels="false"
      :disabled="disabled"
      placeholder="请选择公司部门"
    ></el-cascader>
  </div>
</template>
<script>
import {
  omJobSelectGroup,
  omJobSelectCorpType,
  omJobSelectBdCorp,
  omJobSelectBdDeptdoc
} from "../../api/baseSetting";
export default {
  props: {
    disabled: {
      default: false,
      type: Boolean
    },
    companyDepartData: {
      type: Array
    }
  },
  data() {
    let that = this;
    return {
      value: [],
      sendData: [],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          console.log(6666, level);
          if (level > 0) {
            that.setSendData(level - 1, node.data);
          }
          function formData(arr, label, value) {
            let newArr = [];
            for (let i = 0; i < arr.length; i++) {
              let obj = {
                value: arr[i][value],
                label: arr[i][label],
                leaf: level >= 3
              };
              newArr.push(obj);
            }
            resolve(newArr);
          }
          that.getDepartList();
          if (level == 0) {
            omJobSelectGroup({}).then(res => {
              formData(res, "unitname", "pkCorp");
            });
          } else if (level == 1) {
            omJobSelectCorpType({ pkCorp: node.value }).then(res => {
              formData(res, "unitname", "pkCorp");
            });
          } else if (level == 2) {
            omJobSelectBdCorp({ pkCorp: node.value }).then(res => {
              formData(res, "unitname", "pkCorp");
            });
          } else if (level == 3) {
            omJobSelectBdDeptdoc({ pkCorp: node.value }).then(res => {
              formData(res, "deptname", "pkDeptdoc");
              that.getDepartList(res);
            });
          } else {
            resolve([]);
          }
        }
      },
      departList: []
    };
  },
  created() {
    if (this.companyDepartData && this.companyDepartData.length > 0) {
      this.value = this.companyDepartData;
    }
  },
  methods: {
    getDepartList(list) {
      this.departList = list || [];
    },
    //获取每一次选中的值得label和value
    setSendData(index, data) {
      this.sendData[index] = data;
    },
    //选中最终的值
    handleChange(e) {
      if (this.departList.length > 0 && e[3]) {
        let departId = e[3];
        for (let i = 0; i < this.departList.length; i++) {
          if (departId == this.departList[i].pkDeptdoc) {
            this.setSendData(3, this.departList[i]);
          }
        }
      }
      this.$emit("getCompanyDepart", this.sendData);
    }
  },
  watch: {
    companyDepartData(data) {
      this.value = [...data];

      // console.log(data);
    }
  }
};
</script>
<style lang="less" scoped>
</style>